<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>this测试</title>
</head>
<body>
  <script>
   /*  Function.prototype.bind = function (obj) {
      // 返回一个新函数
      return () => {
        // 新函数内部通过call来调用原函数, 并指定this为obj
        return this.call(obj)
      }
    }


    var m = 2
    function fn() {
      console.log(this.m)
    }
    const obj = {m: 1}
    // fn.call(obj)
    var fn2 = fn.bind(obj)
    console.log(fn2===fn)
    fn2()
    fn.bind(obj)()
    fn() */






    // 题1
    
   /*  var a = 10
    function foo () {
      console.log(this.a)
    }
    foo()  */
   


    // 题2
    
    /* "use strict"
    var a = 10
    function foo () {
      console.log('this1', this)
      console.log(window.a)
      console.log(this.a)
    }
    console.log(window.foo)
    console.log('this2', this)
    foo()  */
   

    // 题3
    
    /* let a = 10
    const b = 20

    function foo () {
      console.log(this.a)
      console.log(this.b)
    }
    foo();
    console.log(window.a)  */
   

    // 题4
    /* 
    var a = 1
    function foo () {
      var a = 2
      function inner () { 
        console.log(this.a)
      }
      inner()
    }
    foo() 
    */

    // 题5
    /* 
    function foo () {
      console.log(this.a)
    }
    var obj = { a: 1, foo }
    var a = 2
    obj.foo() 
    */

    // 题6
    /* 
    function foo () {
      console.log(this.a)
    };
    var obj = { a: 1, foo }
    var a = 2
    var foo2 = obj.foo
    var obj2 = { a: 3, foo2: obj.foo }

    obj.foo()
    foo2()
    obj2.foo2() 
    */


    // 题7
    /* 
    function foo () {
      console.log(this.a)
      return function () {
        console.log(this.a)
      }
    }
    var obj = { a: 1 }
    var a = 2

    foo()
    foo.call(obj)
    foo().call(obj) 
    */


    // 题8
   /*  
   function foo () {
      console.log(this.a)
      return function () {
        console.log(this.a)
      }
    }
    var obj = { a: 1 }
    var a = 2

    foo()
    foo.bind(obj)
    foo().bind(obj) 
    */

    // 题9
    /* 
    var obj = {
      name: 'obj',
      foo1: () => {
        console.log(this.name)
      },
      foo2: function () {
        console.log(this.name)
        return () => {
          console.log(this.name)
        }
      }
    }
    var name = 'window'
    obj.foo1()
    obj.foo2()() 
    */
  </script>
</body>
</html>